package cn.idu.socket;

import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.nio.charset.StandardCharsets;

public class TcpClientDemo01 {

    public static void main(String[] args) {
        Socket socket = null;
        OutputStream outputStream = null;
        try {
            //1. 定义ip和port
            InetAddress serverIp = InetAddress.getLocalHost();
            int port = 9999;

            //2. 创建socket连接
            socket = new Socket(serverIp, port);

            //3. 发送消息IO流
            outputStream = socket.getOutputStream();
            outputStream.write("hello".getBytes(StandardCharsets.UTF_8));

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                outputStream.close();
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
